package net.lcheng.shop.Controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.lcheng.model.*;
import net.lcheng.service.*;
import net.lcheng.shop.vo.CurrentUserModel;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;
/**
 * Created by zwr on 2016/6/28 0028.
 */
@Controller
public class HomeController {
    @Autowired
    private PProductService pProductService;
    @Autowired
    private WxShopBannerService wxShopBannerService;
    @Autowired
    private WxShopService shopService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserAgentService userAgentService;
    @Autowired
    private WxShopCategoryService wxShopCategoryService;

    @RequestMapping("/home")
    public String home(Model model){
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser.isAuthenticated()) {
            //获取当前用户
            Session session = currentUser.getSession();
            CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            //查看用户是不是有店铺如果有店铺就显示店铺设置
            WxShop shop = shopService.getShopInforByUserId(currentUserModel.getUserId());
            if (shop != null) {
                return "redirect:/"+shop.getShopId();
            }
        }
        return "redirect:/1";
    }

    /**
     * 店铺首页
     * @param model
     * @param shopId
     * @return
     */
    @RequestMapping("/{shopId}")
    public String shopHome(Model model, @PathVariable(value = "shopId") Integer shopId) {//@RequestParam Integer shopId
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser.isAuthenticated()) {
            //获取当前用户
            Session session = currentUser.getSession();
            CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            User user=userService.getUserByUserId(currentUserModel.getUserId());
            //判断用户有没有登录
            if (user==null){
                SecurityUtils.getSubject().logout();
                return "redirect:/login";
            }
            //判断用户有没有进行微店申请申请了就不显示这个按钮
            WxShop shopByUserId = shopService.getShopInforByUserId(currentUserModel.getUserId());
            if (shopByUserId==null){
                model.addAttribute("shopByUserIdentity",1);
            }


            //判断用户有没有进行店铺申请申请了就消失申请按钮
            UserAgent userAgentByUserId=userAgentService.getUserAgentByUserId(user.getUserId());
            if (userAgentByUserId==null){
                model.addAttribute("userAgentByUserIdentity",2);
            }
        //获取店铺信息
        WxShop shop = shopService.getWxShopByShopId(shopId);
        model.addAttribute("shop",shop);

            /**查看该用户是不是运营中的商铺 create by LV**/
            WxShop wxShop = shopService.getShopInforByUserId(currentUserModel.getUserId());
            if (wxShop != null) {
                if (wxShop.getShopStatus()==2)
                    model.addAttribute("isSeller",1);
                else
                    model.addAttribute("isSeller",0);
            }else {
                model.addAttribute("isSeller",0);
            }

        //获取店铺id(店铺首页--店铺推荐)
        List<PProduct> pProductList = pProductService.getPProductByShopId(shopId);
        model.addAttribute("pProductList", pProductList);
        //获取店铺id(店铺首页--店铺推荐)
        List<PProduct> saleCountTotal = pProductService.getPProductBySaleCountTotal(4, shopId);
        //判断list有几条数据如果有1条不显示如果大于2条小于4条就显示2条(2的倍数)
        int SaleCountTotal = saleCountTotal.size();
        if (SaleCountTotal == 4) {
            model.addAttribute("saleCountTotal", saleCountTotal);
        } else if (SaleCountTotal > 2 && SaleCountTotal < 4) {
            model.addAttribute("saleCountTotal", saleCountTotal.subList(0, 2));
        } else
            model.addAttribute("saleCountTotal", null);
        /**获取店铺Banner从排序(order_num)从大到小排序*/
        List<WxShopBanner>wxShopBannerList= wxShopBannerService.getBannerListByShopId(shopId);
        model.addAttribute("wxShopBannerList",wxShopBannerList);
        model.addAttribute("shopId",shopId);
        }else
            return "redirect:/login";

        return "home";
    }

    @RequestMapping(value = "/test")
    public String testUpload(Model model){
        return "test";
    }

    /**
     * 全部分类
     * @param model
     * @param shopId
     * @return
     */
    @RequestMapping("/{shopId}/list")
    public String shopList(Model model, @PathVariable(value = "shopId") Integer shopId,HttpServletRequest request) {
        if (shopId==null){
            return "redirect:/home";
        }
        String productName = request.getParameter("productName");
        //获取店铺分类列表
        List<WxShopCategory>categoryList=wxShopCategoryService.getWxShopCategoryByShopId(shopId);
        if (categoryList.isEmpty()){
            return "shop_list";
        }
          //获取默认图片
            List<PProduct>  pProduct=pProductService.pProductByPProductId(categoryList.get(0).getShopId(),categoryList.get(0).getCategoryId(),1);
            model.addAttribute("PProduct",0);
            model.addAttribute("category",0);
            model.addAttribute("pProduct",pProduct);
            model.addAttribute("categoryList",categoryList);
        //获取商品判断用户有没有搜索
        if (StringUtils.isEmpty(productName)==false){
            List<PProduct>pProductList=pProductService.getPProductByShopIdAndProductName(shopId,productName,1);
            if (pProductList.isEmpty()){
                model.addAttribute("category",1);
                return "shop_list";
            }
            //获取搜索商品的分类
            WxShopCategory wxShopCategory=wxShopCategoryService.getWxShopCategoryByCategoryId(pProductList.get(0).getWxShopCategoryId());
            if (wxShopCategory==null){
                return "shop_list";
            }
            model.addAttribute("wxShopCategory",wxShopCategory);
            model.addAttribute("PProduct",1);
            model.addAttribute("pProductList",pProductList);
        }
        model.addAttribute("productName", productName);
        model.addAttribute("shopId",shopId);
        return "shop_list";
    }
    /**
     * 获取全部分类图片
     * @param categoryId
     * @param shopId
     * @return
     */
     @RequestMapping(value = "/classification" ,method = RequestMethod.GET)
     @ResponseBody
     public String getParentCodeList(@RequestParam String categoryId, @RequestParam Integer shopId){
         List<PProduct>pProductList=pProductService.pProductByPProductId(shopId,categoryId,1);
         return JSON.toJSONString(pProductList);
     }
}